<template>
    <rx-dialog @handOk="handleSubmit" @cancel="cancel">
        <rx-layout>
            <div slot="center">
                <a-form :form="form" layout="vertical" style="padding: 12px">
                    <a-form-item style="display:none">
                        <a-input v-decorator="['seqId']"/>
                    </a-form-item>
                    <a-form-item style="display:none">
                        <a-input v-decorator="['ruleConf']"/>
                    </a-form-item>
                    <a-form-item style="display:none">
                        <a-input v-decorator="['confAttr']"/>
                    </a-form-item>
                    <a-row :gutter="24">
                        <a-col :span="12">
                            <a-form-item  :label="i18n('form.name','名称')">
                                <a-input @blur.native="getPinyinByName" :placeholder="i18n('form.name','名称','input')"
                                         v-decorator="['name', {rules: [{required: true, message: i18n('form.name','名称','input')}]}]"/>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item  :label="i18n('form.key','别名')" class=" x-a-form-model-error">
                                <a-input :placeholder="i18n('form.key','别名','input')"
                                         v-decorator="['alias', {rules: [
                                             {required: true, message: i18n('form.key','别名','input')},
                                             {pattern:/^[a-zA-Z][a-zA-Z0-9_-]*$/,message:this.i18n2('form.str','不能以数字开头,不能包含【_-】以外的特殊字符！','input')}
                                             ]}]"/>
                            </a-form-item>
                        </a-col>
                    </a-row>

                    <a-row :gutter="24">
                        <a-col :span="12">
                            <a-form-item
                                         :label="i18n('form.classificationSelection','分类选择')">
                                <rx-category-select cat-key="FORM" v-if="isShowManageTree" :isAdmin="isManageToTree"
                                                    :isGrant="true"
                                                    v-decorator="['treeId', {rules: [{required: true, message: i18n('form.classification','分类','select')}]}]"></rx-category-select>
                                <a-input v-else style="display:none;" v-decorator="['treeId']"/>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item
                                         :label="i18n('form.systemDefault','系统缺省')">
                                <a-radio-group
                                    v-decorator="['isDefault', {initialValue:'YES'}, {rules: [{required: false, message: this.$ti18('rxDecorator.select','请选择')}]}]">
                                    <a-radio value="YES">
                                        {{ $comi18('business.deleteMap.yes', '是') }}
                                    </a-radio>
                                    <a-radio value="NO">
                                        {{ $comi18('business.deleteMap.no', '否') }}
                                    </a-radio>
                                </a-radio-group>
                            </a-form-item>
                        </a-col>
                    </a-row>

                    <a-row :gutter="24">
                        <a-col :span="24">
                            <a-form-item
                                         :label="i18n('form.rule','规则')">
                                <a-select :placeholder="i18n('form.ruleType','规则类型','select')"
                                          key="ruleType"
                                          v-model="text"
                                          :getPopupContainer="p=>p.parentNode"
                                          @change="handleChange"
                                >
                                    <a-select-option v-for="type in typeData" :value="type.id" :key="type.id">
                                        {{ type.text }}
                                    </a-select-option>
                                </a-select>

                                <a-input style="margin-top: 10px;" :placeholder="i18n('form.serialNumberRules','流水号规则')"
                                         v-decorator="['rule', {rules: [{required: true, message: i18n('form.serialNumberRules','请输入流水号规则','input')}]}]"/>
                            </a-form-item>
                        </a-col>
                    </a-row>

                    <a-row :gutter="24">
                        <a-col :span="12">
                            <a-form-item
                                         :label="i18n('form.generateMethod','生成方式')">
                                <a-radio-group
                                    v-decorator="['genType', {initialValue:'DAY',rules: [{required: true, message: i18n('form.generateMethod','生成方式','select')}]}]">
                                    <a-radio :value="'DAY'">
                                        {{ i18n('form.daily', '每天') }}
                                    </a-radio>
                                    <a-radio :value="'WEEK'">
                                        {{ i18n('form.weekly', '每周') }}
                                    </a-radio>
                                    <a-radio :value="'MONTH'">
                                        {{ i18n('form.monthly', '每月') }}
                                    </a-radio>
                                    <a-radio :value="'YEAR'">
                                        {{ i18n('form.annually', '每年') }}
                                    </a-radio>
                                    <a-radio :value="'AUTO'">
                                        {{ i18n('form.increment', '自动增长') }}
                                    </a-radio>
                                </a-radio-group>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item
                                         :label="i18n('form.serialNumberLength','流水号长度')">
                                <a-input-number :min="2" :max="1000000"
                                                :placeholder="i18n('form.serialNumberLength','流水号长度')"
                                                v-decorator="['len', {initialValue:5}, {rules: [{required: false, message: this.i18n('form.serialNumberLength','请输入流水号长度','input')}]}]"/>
                            </a-form-item>
                        </a-col>
                    </a-row>
                    <a-row :gutter="24">
                        <a-col :span="12">
                            <a-form-item :label="i18n('form.initialValue','初始值')">
                                <a-input-number :min="1" :max="1000000" :placeholder="i18n('form.initialValue','初始值')"
                                                v-decorator="['initVal',{initialValue:1}, {rules: [{required: false, message:  this.i18n('form.initialValue','初始值','input')}]}]"/>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item  :label="i18n('form.step','步长')">
                                <a-input-number :min="1" :max="1000000" :placeholder="i18n('form.step','步长')"
                                                v-decorator="['step',{initialValue:1}, {rules: [{required: false, message: this.i18n('form.step','步长','input')}]}]"/>
                            </a-form-item>
                        </a-col>
                    </a-row>

                    <a-row :gutter="24">
                        <a-col :span="24">
                            <a-form-item :label="$ti18('rxForm.notes','备注')">
                                <a-textarea :placeholder="$ti18('rxForm.notes','备注')"
                                            v-decorator="['memo', {rules: [{required: false, message: this.i18n('rxForm.notes','备注','input')}]}]"
                                            :rows="4"/>
                            </a-form-item>
                        </a-col>
                    </a-row>

                </a-form>
            </div>
        </rx-layout>

    </rx-dialog>
</template>
<script>
import SysSeqIdApi from '@/api/system/core/sysSeqId'
import {BaseForm, RxDialog} from "@lowcode/jpaas-common-lib";

export default {
    name: 'SysSeqIdEdit',
    mixins: [BaseForm],
    props: {
        layerid: String,
        lydata: Object,
        destroy: Function
    },
    data() {
        return {
            typeData: [
                {id: '{yyyyMMdd}', text: this.i18n('form.text1', '年月日-yyyyMMdd(如20160102)')},
                {id: '{yyyyMM}', text: this.i18n('form.text2', '年月-yyyyMM(如201601)')},
                {id: '{yyyy}', text: this.i18n('form.text3', '年-yyyy(如2016)')},
                {id: '{yy}', text: this.i18n('form.text4', '年-yy(如16)')},
                {id: '{MM}', text: this.i18n('form.text5', '月-MM(如03)')},
                {id: '{mm}', text: this.i18n('form.text6', '月-mm(如3)')},
                {id: '{DD}', text: this.i18n('form.text7', '日DD-(如03)')},
                {id: '{dd}', text: this.i18n('form.text8', '日dd-(如3)')},
                {id: '{NO}', text: this.i18n('form.text9', '序号-NO(如0003)')},
                {id: '{no}', text: this.i18n('form.text10', '序号-no(如3)')},
                {id: '{KEY}', text: this.i18n('form.text11', '外部变量')}
            ],
            idx_: 1
        }
    },
    created() {
        this.isUserManager(this.$route.query.appId);
    },
    components: {
        RxDialog,
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "sysSeqId", key);
        },
        i18n2(name,text,key){
            return this.$ti18(name,text,"formBoEntity",key);
        },
        get(id) {
            return SysSeqIdApi.get(id);
        },
        save(values) {
            values.appId = this.$parent.curApp.appId;
            return SysSeqIdApi.save(values);
        },
        getPinyinByName() {
            let name = this.form.getFieldsValue().name
            if (!name) {
                return;
            }
            if (this.form && this.form.getFieldsValue().alias) {
                return;
            }
            this.getPinyin(name).then(res => {
                //去除不可见的特殊符号
                if (res) {
                    res = res.replace(/\u200B/g, '');
                }
                this.form.setFieldsValue({alias: res})
            })
        },
        handleChange(value) {
            if (value) {
                var rule = this.form.getFieldValue("rule") ? this.form.getFieldValue("rule") : "";
                this.form.setFieldsValue({
                    rule: rule + value
                });
            }
        }
    }
}
</script>
